const Biz = require('../biz/index');
const fw = require('../core/framework');
const exception = require('../exception');

function setFaceTimestamp(successFn,errorFn){
    Biz.Config.save({code:'face',value:new Date().getTime(),name:'人脸变化时间戳',remark:'识别人脸是否有变化'},function(){
        if(fw.isFn(successFn))successFn();
    },function(){
        if(fw.isFn(errorFn))errorFn();
    })
}

module.exports = {
    init(router,urlencodedParser){
        router.post('/face/create', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                let faceData = {
                    name:param.name,
                    eigen_value:param.eigen_value,
                    age:param.age,
                    sex:param.sex,
                    url:param.url,
                    remark:param.remark,
                    merchant_id:fw.parseMerchantId(req)
                };
                Biz.Face.create(faceData,(DBRes)=>{
                    setFaceTimestamp(function(){
                        res.send(DBRes);
                    },function(){
                        res.send(DBRes);
                    });
                },(DBRes)=>{
                    res.send(DBRes);
                });
            });
        });
        router.post('/face/update', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                if(fw.isExit(param.id)){
                    Biz.Face.update(param,(data)=>{
                        setFaceTimestamp(function(){
                            res.send(data);
                        },function(){
                            res.send(data);
                        });
                    },(data)=>{
                        res.send(data);
                    });
                }else{
                    res.send(exception.paramEx('id'));
                }
            });
        });
        router.post('/face/delete', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                if(fw.isExit(param.id)){
                    Biz.Face.delete(param,(data)=>{
                        setFaceTimestamp(function(){
                            res.send(data);
                        },function(){
                            res.send(data);
                        });
                    },(data)=>{
                        res.send(data);
                    });
                }else{
                    res.send(exception.paramEx('id'));
                }
            });
        });
        router.post('/face/search', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                param.merchant_id = fw.parseMerchantId(req);
                Biz.Face.search(param,(data)=>{
                    res.send(data);
                },(data)=>{
                    res.send(data);
                });
            });
        });

        router.post('/face/useByAi/search', urlencodedParser, function(req, res) {
            let param = fw.utils.parseBody(req);
            Biz.Config.hasNewFace({code:'face',value:param.timestamp},function(timeRes){
                Biz.Face.searchAllToAI((data)=>{
                    res.send({
                        code:0,
                        data:{
                            timestamp:timeRes.value,
                            content:data.data
                        }
                    });
                },(data)=>{
                    res.send(data);
                });
            },function(){
                res.send(exception['1000']);
            })
        });
    }
};
